package cn.itcast.n4;

import java.util.concurrent.TimeUnit;

/**
 * @author AirMan
 * @version 1.0
 * @description
 * @date 2025/4/7 18:54
 */
public class sleepAndYield {
    public static void main(String[] args) {
        Runnable task = () -> {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "=>" + i);
                // 主动让出cpu
//                Thread.yield();
                // 强制放弃cpu
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        };

        Thread t1 = new Thread(task, "t1");
        Thread t2 = new Thread(task, "t2");

        t1.start();
        t2.start();

    }
}
